/**
 * 
 */
package com.blog.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blog.mapper.SysUserRoleMapper;
import com.blog.pojo.SysUser;
import com.blog.pojo.SysUserRole;
import com.blog.pojo.SysUserRoleExample;
import com.blog.service.SysUserRoleService;

/**
 * @author zyj
 *
 */
@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {
	
	@Autowired
	private SysUserRoleMapper SysUserRoleMapper;

	/* 
	 * �����û���Ȩ��
	 */
	@Override
	public void setRoles(SysUser user, long[] roleIds) {
		//ɾ����ǰ�û������н�ɫ
		SysUserRoleExample example = new SysUserRoleExample();
		example.createCriteria().andIdEqualTo(user.getId());
		List<SysUserRole> urs = SysUserRoleMapper.selectByExample(example);
		for (SysUserRole SysUserRole : urs) {
			SysUserRoleMapper.deleteByPrimaryKey(SysUserRole.getId());
		}
		//�����µĽ�ɫ��ϵ
		if(null!=roleIds) {
			for (Long rid : roleIds) {
				SysUserRole SysUserRole = new SysUserRole();
				SysUserRole.setRoleId(rid);
				SysUserRole.setUserId(user.getId());
                SysUserRoleMapper.insert(SysUserRole);
			}
		}
	}

	/* 
	 * ɾ���û�
	 */
	@Override
	public void deleteBySysUser(long userId) {
		SysUserRoleExample example= new SysUserRoleExample();
        example.createCriteria().andIdEqualTo(userId);
        List<SysUserRole> urs= SysUserRoleMapper.selectByExample(example);
        for (SysUserRole SysUserRole : urs) {
            SysUserRoleMapper.deleteByPrimaryKey(SysUserRole.getId());
        }
	}

	/* 
	 * ɾ����ɫ
	 */
	@Override
	public void deleteByRole(long roleId) {
		SysUserRoleExample example= new SysUserRoleExample();
        example.createCriteria().andIdEqualTo(roleId);
        List<SysUserRole> urs= SysUserRoleMapper.selectByExample(example);
        for (SysUserRole SysUserRole : urs) {
            SysUserRoleMapper.deleteByPrimaryKey(SysUserRole.getId());
        }
	}

}
